from hanchlogger import logger
import openpyxl
import hanchxl
import time
import ReplaceBuMen


class infoJieDanFromQuanBu(object):
    def __init__(self, name_keynote='', label_keynote=3):
        self.CountJieDanRi = 0                      				# 日接单量
        self.CountJieDanRiQu = 0                    				# 日接单量区派
        self.CountJieDanRiShi = 0                   				# 日接单量市派
        self.CountJieDanAll = 0                     				# 本评价期接单量
        self.CountJieDanAllQu = 0                   				# 本评价期接单量区派
        self.CountJieDanAllShi = 0                  				# 本评价期接单量市派
        self.CountKeynoteName = name_keynote   				        # 重点问题名称
        self.CountKeynoteLabel = label_keynote                      # 重点问题标签
        self.CountKeynoteRi = 0                   				    # 重点问题日接单量
        self.CountKeynoteRiQu = 0                   				# 重点问题日接单量区派
        self.CountKeynoteRiShi = 0                  				# 重点问题日接单量市派
        self.CountKeynoteAll = 0                                    # 重点问题本评价期接单量
        self.CountKeynoteAllQu = 0                                  # 重点问题本评价期接单量区派
        self.CountKeynoteAllShi = 0                                 # 重点问题本评价期接单量市派
        self.DirectoryChengBanAll = {}                              # 承办部门本评价期接单量
        self.DirectoryChengBanRi = {}                               # 承办部门日接单量

    def load_info_from_quan_bu(self, sheet_quan_bu=None, list_replace=None, start_jie_dan=0, end_jie_dan=0, start_ping_jia=0, end_ping_jia=0):
        if sheet_quan_bu is not None:
            maxRow = sheet_quan_bu.max_row
            for buMen in list_replace:
                if buMen.isAll is True:
                    hanchxl.xiu_gai_cheng_ban_bu_men_all(sheet_quan_bu, '承办部门', buMen.nameYuan, buMen.nameNow)
                    hanchxl.xiu_gai_cheng_ban_bu_men_all(sheet_quan_bu, '参与部门', buMen.nameYuan, buMen.nameNow)
                else:
                    hanchxl.xiu_gai_cheng_ban_bu_men(sheet_quan_bu, '承办部门', buMen.nameYuan, buMen.nameNow)
                    hanchxl.xiu_gai_cheng_ban_bu_men(sheet_quan_bu, '参与部门', buMen.nameYuan, buMen.nameNow)
            hanchxl.remove_repeat_value(sheet_quan_bu, '承办部门', '参与部门')
            colCode = hanchxl.get_col_index(sheet_quan_bu, '工单编号')
            colChengBan = hanchxl.get_col_index(sheet_quan_bu, '承办部门')
            colLeiXing = hanchxl.get_col_index(sheet_quan_bu, '派单类型')
            colLabel = hanchxl.get_col_index(sheet_quan_bu, '标签')
            colPaiDan = hanchxl.get_col_index(sheet_quan_bu, '派单时间')
            for rowIndex in range(2, maxRow + 1):
                valueCode = '{}'.format(sheet_quan_bu.cell(rowIndex, colCode).value).strip()
                valuePaiDan = '{}'.format(sheet_quan_bu.cell(rowIndex, colPaiDan).value).strip()
                valueLabel = '{}'.format(sheet_quan_bu.cell(rowIndex, colLabel).value).strip()
                valueLeiXing = '{}'.format(sheet_quan_bu.cell(rowIndex, colLeiXing).value).strip()
                valueChengBan = '{}'.format(sheet_quan_bu.cell(rowIndex, colChengBan).value).strip()
                timeValuePaiDanFormat = time.strptime(valuePaiDan, '%Y-%m-%d %H:%M:%S')
                timeStampValuePaiDan = int(time.mktime(timeValuePaiDanFormat))
                logger.debug('{}行  工单编号：{}  派单时间：{}  工单类型：{} '.format(rowIndex, valueCode, valuePaiDan, valueLeiXing))
                if start_ping_jia <= timeStampValuePaiDan < end_ping_jia:
                    self.CountJieDanAll = self.CountJieDanAll + 1
                    logger.debug('本评价期接单 {}件  {}行  工单编号：{}  派单时间：{}  工单类型：{} '.format(self.CountJieDanAll, rowIndex, valueCode, valuePaiDan, valueLeiXing))
                    if valueLeiXing == "直派":
                        self.CountJieDanAllShi = self.CountJieDanAllShi + 1
                    elif valueLeiXing == "转派":
                        self.CountJieDanAllQu = self.CountJieDanAllQu + 1
                    if valueChengBan in self.DirectoryChengBanAll:
                        self.DirectoryChengBanAll[valueChengBan] = self.DirectoryChengBanAll[valueChengBan] + 1
                    else:
                        self.DirectoryChengBanAll[valueChengBan] = 1
                    if self.CountKeynoteName != '' and valueLabel == self.CountKeynoteLabel:
                        self.CountKeynoteAll = self.CountKeynoteAll + 1
                        if valueLeiXing == "直派":
                            self.CountKeynoteAllShi = self.CountKeynoteAllShi + 1
                        elif valueLeiXing == "转派":
                            self.CountKeynoteAllQu = self.CountKeynoteAllQu + 1
                if start_jie_dan <= timeStampValuePaiDan < end_jie_dan:
                    self.CountJieDanRi = self.CountJieDanRi + 1
                    logger.debug('今日接单 {}件 {}行  工单编号：{}  派单时间：{}  工单类型：{} '.format(self.CountJieDanRi, rowIndex, valueCode, valuePaiDan, valueLeiXing))
                    if valueLeiXing == "直派":
                        self.CountJieDanRiShi = self.CountJieDanRiShi + 1
                    elif valueLeiXing == "转派":
                        self.CountJieDanRiQu = self.CountJieDanRiQu + 1
                    if self.CountKeynoteName != '' and valueLabel == self.CountKeynoteLabel:
                        self.CountKeynoteRi = self.CountKeynoteRi + 1
                        if valueLeiXing == "直派":
                            self.CountKeynoteRiShi = self.CountKeynoteRiShi + 1
                        elif valueLeiXing == "转派":
                            self.CountKeynoteRiQu = self.CountKeynoteRiQu + 1
                    if valueChengBan in self.DirectoryChengBanRi:
                        self.DirectoryChengBanRi[valueChengBan] = self.DirectoryChengBanRi[valueChengBan] + 1
                    else:
                        self.DirectoryChengBanRi[valueChengBan] = 1

            self.DirectoryChengBanRi = dict(sorted(self.DirectoryChengBanRi.items(), key=lambda kv: kv[1], reverse=True))
            self.DirectoryChengBanAll = dict(sorted(self.DirectoryChengBanAll.items(), key=lambda kv: kv[1], reverse=True))
        return

